package de.homelabs.modules.menu.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.WebApplicationContext;

import de.homelabs.modules.menu.domain.HLMenuEntry;

@Service("hlMenuManager")
public class HLMenuManager implements IHLMenuManager {
	
	//Singeltion Design Pattern
	private static HLMenuManager instance = new HLMenuManager();
		
	//vars
	private WebApplicationContext webAppContext = null;
	private String contextPath = null;
	private List<HLMenuEntry> mainMenu = new ArrayList<HLMenuEntry>();
	private List<HLMenuEntry> subMenu = new ArrayList<HLMenuEntry>();
	
	/**
	 * private constructor for singleton pattern
	 */
	private HLMenuManager(){}
	
	/**
	 * alternate constructor function
	 */
	public HLMenuManager getInstance(){
		return instance;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
	 */
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		
		//we need the webapplicationcontext to get the contextPath
		if (applicationContext != null && applicationContext instanceof WebApplicationContext){
			WebApplicationContext webAppContext = (WebApplicationContext) applicationContext;
			this.webAppContext = webAppContext;
			this.contextPath = this.webAppContext.getServletContext().getContextPath();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see de.homelabs.menu.service.IHLMenuManager#getMainMenuEntries()
	 */
	public List<HLMenuEntry> getMainMenuEntries() {
		return this.getMainMenuEntries();
	}

	/*
	 * (non-Javadoc)
	 * @see de.homelabs.menu.service.IHLMenuManager#getSubMenuEntries()
	 */
	public List<HLMenuEntry> getSubMenuEntries() {
		return this.getSubMenuEntries();
	}

	public String getContextPath(){
		return this.contextPath;
	}
	
	/*
	 * (non-Javadoc)
	 * @see de.homelabs.menu.service.IHLMenuManager#addMainmenuEntry(de.homelabs.menu.domain.HLMenuEntry)
	 */
	public boolean addMainmenuEntry(HLMenuEntry menuEntry) {
		this.mainMenu.add(menuEntry);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see de.homelabs.menu.service.IHLMenuManager#removeMainmenuEntry(de.homelabs.menu.domain.HLMenuEntry)
	 */
	public boolean removeMainmenuEntry(HLMenuEntry menuEntry) {
		//loop over mainmenu
		for (HLMenuEntry e : this.mainMenu){
			if (e.equals(menuEntry)){
				this.mainMenu.remove(menuEntry);
				return true;
			}
		}
		
		//no entry found
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see de.homelabs.menu.service.IHLMenuManager#addSubmenuEntry(de.homelabs.menu.domain.HLMenuEntry)
	 */
	public boolean addSubmenuEntry(HLMenuEntry menuEntry) {
		this.subMenu.add(menuEntry);
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see de.homelabs.menu.service.IHLMenuManager#removeSubmenuEntry(de.homelabs.menu.domain.HLMenuEntry)
	 */
	public boolean removeSubmenuEntry(HLMenuEntry menuEntry) {
		//loop over mainmenu
		for (HLMenuEntry e : this.subMenu){
			if (e.equals(menuEntry)){
				this.subMenu.remove(menuEntry);
				return true;
			}
		}
		
		//no entry found
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see de.homelabs.menu.service.IHLMenuManager#renderMainmenu()
	 */
	public StringBuffer renderMainmenu() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see de.homelabs.menu.service.IHLMenuManager#renderSubmenu()
	 */
	public StringBuffer renderSubmenu() {
		// TODO Auto-generated method stub
		return null;
	}
}
